package com.kaifamiao.QuickHit;

import java.util.Scanner;

public class Game {

    public Player player;

    public Game(Player player) {
        this.player = player;
    }

    public Game() {
    }

    public String printStr() {
        StringBuilder sb = new StringBuilder();
        int strLength = LevelArrayList.levelArrays[player.getLevelNo() - 1].getStrLength(); // 获取当前等级一次输入字符串的长度
        for (int i = 0; i < strLength; i++) {
            int rand = (int) Math.round(Math.random() * 10);
            switch (rand) {
                case 0 -> sb.append("a");
                case 1 -> sb.append("b");
                case 2 -> sb.append("c");
                case 3 -> sb.append("d");
                case 4 -> sb.append("e");
                case 5 -> sb.append("f");
                case 6 -> sb.append("g");
                case 7 -> sb.append("h");
                case 8 -> sb.append("j");
                case 9 -> sb.append("k");
                default -> sb.append("l");
            }
        }
        System.out.println(sb);
        return sb.toString();
    }

    /**
     * 判断用户输入与系统出现的字符串的结果
     *
     * @param printStr 系统随机的字符串
     * @param inputStr 用户输入的字符串
     */
    public void printResult(String printStr, String inputStr) {
        player.setEndTime(System.currentTimeMillis());
        if (printStr.equals(inputStr)) {
            int time = (int) (player.getEndTime() - player.getStartTime()) / 1000; // 游戏耗费的时间
            int limitTime = LevelArrayList.levelArrays[player.getLevelNo() - 1].getTimeLimit(); // 当前等级的时间限制
            int score = LevelArrayList.levelArrays[player.getLevelNo() - 1].getPerScore(); // 当前等级的每次得分
            if (time > limitTime) {
                System.out.println("输入时间超时！");
                System.out.println("******************");
                System.out.println("****Game over!****");
                System.out.println("******************");
                System.exit(1);  // 输入超时，系统异常退出
            } else {
                player.setCutScore(player.getCutScore() + score);
                player.setElapsedTime(time);
                System.out.println("**************************");
                System.out.println("输入正确！");
                System.out.println("当前级别： " + player.getLevelNo() + ",");
                System.out.println("当前积分： " + player.getCutScore() + ",");
                System.out.println("已用时间： " + player.getElapsedTime() + "ms");
                System.out.println("*************************");
            }
        } else {
            System.out.println("输入不正确！");
            System.out.println("你有三次复活机会，当前剩余：" + player.getReborn() + "次");
            System.out.println("是否重新开始？y 是 / 任意字符退出");
            Scanner in = new Scanner(System.in);
            String e = in.next();
            if ("y".equals(e)) {
                if (player.getReborn() > 0) {
                    player.setReborn(player.getReborn() - 1);
                    player.play();
                } else {
                    System.out.println("您当前没有复活机会了，请充值！！");
                }
            } else {
                System.out.println("******************");
                System.out.println("****Game over!****");
                System.out.println("******************");
                System.exit(0); // 输入错误并且不复活，系统正常退出
            }

        }
    }
}
